package com.zhiyizhen.handheld.component.pharmacy.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.zhiyizhen.handheld.component.pharmacy.bean.GroupDetailListBean;
import com.zhiyizhen.handheld.component.pharmacy.bean.GroupListBean;
import com.zhiyizhen.handheld.component.pharmacy.bean.RegimenRecordBean;
import com.zhiyizhen.handheld.databinding.ItemRegimenRecordChengyaoBinding;
import com.zhiyizhen.handheld.databinding.ItemRegimenRecordOtherBinding;
import com.zhiyizhen.handheld.databinding.ItemRegimenRecordShuyeBinding;

import java.util.List;

public class RegimenRecordDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    // 中西医成药
    private static final int TYPE_CHENGYAO = 1;
    // 输液
    private static final int TYPE_SHUYE = 2;
    // 其他
    private static final int TYPE_OTHER = 3;
    private final Context context;
    private List<RegimenRecordBean.RxListBean> mDatas;

    public RegimenRecordDetailAdapter(Context context, List<RegimenRecordBean.RxListBean> mDatas) {
        this.context = context;
        this.mDatas = mDatas;
    }

    public class ChengyaoViewHolder extends RecyclerView.ViewHolder {
        private final ItemRegimenRecordChengyaoBinding binding;

        public ChengyaoViewHolder(ItemRegimenRecordChengyaoBinding viewBinding) {
            super(viewBinding.getRoot());
            binding = viewBinding;
        }

        public void bindData(RegimenRecordBean.RxListBean data) {
            binding.rxTypeTv.setText(data.getRxName());

            List<RegimenRecordBean.DetailListBean> detailList = data.getDetailList();
            StringBuilder drugInfoSb = new StringBuilder();
            RegimenRecordBean.DetailListBean detailListBean;
            if (detailList != null && !detailList.isEmpty()) {
                for (int i = 0; i < detailList.size(); i++) {
                    detailListBean = detailList.get(i);
                    drugInfoSb.append(detailListBean.getDrugName());
                    drugInfoSb.append("   ");
                    if(detailListBean.getDrugDosage() != null) {
                        drugInfoSb.append(detailListBean.getDrugDosage())
                                .append(detailListBean.getDrugDosageUnitText())
                                .append("*");
                    }
                    drugInfoSb.append(detailListBean.getMinPackQty())
                            .append(detailListBean.getMinPackUnitText())
                            .append("/").append(detailListBean.getPackUnitText());
                    drugInfoSb.append("   ");
                    drugInfoSb.append(detailListBean.getCurrentDeliveryQty())
                            .append(detailListBean.getTotalOutQtyUnitText());
                    if (i != detailList.size() - 1) {
                        drugInfoSb.append("\n");
                    }
//                    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
//                    binding.chengyaoLayout.addView(new PharmacyChengyaoUnitLayout(context, i + 1, data.getDetailList().get(i)), layoutParams);
                }
                binding.drugInfoTv.setText(drugInfoSb.toString());
            }
            binding.contentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        }
    }

    public class ShuyeViewHolder extends RecyclerView.ViewHolder {

        private final ItemRegimenRecordShuyeBinding binding;

        public ShuyeViewHolder(ItemRegimenRecordShuyeBinding viewBinding) {
            super(viewBinding.getRoot());
            binding = viewBinding;
        }

        public void bindData(RegimenRecordBean.RxListBean data) {
            binding.contentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            binding.rxTypeTv.setText(data.getRxName());
            StringBuilder drugSb = new StringBuilder();
            List<GroupListBean> groupList = data.getGroupList();
            if (groupList == null || groupList.isEmpty()) {
                return;
            }
            List<GroupDetailListBean> detailList;
            GroupDetailListBean detailListBean;
            for (int i = 0; i < groupList.size(); i++) {
                drugSb.append(groupList.get(i).getGroupName()).append("\n");
                detailList = groupList.get(i).getDetailList();
                if (detailList == null || detailList.isEmpty()) {
                    continue;
                }
                for (int j = 0; j < detailList.size(); j++) {
                    detailListBean = detailList.get(j);
                    drugSb.append(detailListBean.getDrugName()).append("  ");
                    if(detailListBean.getDrugDosage() != null) {
                        drugSb.append(detailListBean.getDrugDosage())
                                .append(detailListBean.getDrugDosageUnitText())
                                .append("*");
                    }
                    drugSb.append(detailListBean.getMinPackQty())
                            .append(detailListBean.getMinPackUnitText())
                            .append("/")
                            .append(detailListBean.getPackUnitText());
                    drugSb.append("   ");
                    drugSb.append(detailListBean.getCurrentDeliveryQty())
                            .append(detailListBean.getTotalOutQtyUnitText());
                    if (j < detailList.size() - 1) {
                        drugSb.append("\n");
                    }
                }
                if (i < groupList.size() - 1) {
                    drugSb.append("\n");
                }
            }
            binding.drugInfoTv.setText(drugSb.toString());

        }
    }

    public class OtherViewHolder extends RecyclerView.ViewHolder {
        private final ItemRegimenRecordOtherBinding binding;

        public OtherViewHolder(ItemRegimenRecordOtherBinding viewBinding) {
            super(viewBinding.getRoot());
            binding = viewBinding;
        }

        public void bindData(RegimenRecordBean.RxListBean data) {
            binding.rxTypeTv.setText(data.getRxName());

            if (data.getTcmInfo() != null) {
                binding.currentTv.setText("本次配：" + data.getTcmInfo().getCurrentDeliveryQty() + "剂");

                GridLayoutManager layoutManager = new GridLayoutManager(context, 3);
                layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                binding.recyclerView.setLayoutManager(layoutManager);
                RegimenRecordOtherInfoAdapter adapter = new RegimenRecordOtherInfoAdapter(context);
                adapter.setData(data.getTcmInfo().getDetailList());
                binding.recyclerView.setAdapter(adapter);
            }
            binding.contentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
        }
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (TextUtils.equals(mDatas.get(position).getRxType(), "1")) {
            return TYPE_CHENGYAO;
        }
        if (TextUtils.equals(mDatas.get(position).getRxType(), "2")) {
            return TYPE_SHUYE;
        }
        return TYPE_OTHER;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int viewType) {
        if (viewType == TYPE_CHENGYAO) {
            return new ChengyaoViewHolder(ItemRegimenRecordChengyaoBinding.inflate(LayoutInflater.from(viewGroup.getContext())));
        } else if (viewType == TYPE_SHUYE) {
            return new ShuyeViewHolder(ItemRegimenRecordShuyeBinding.inflate(LayoutInflater.from(viewGroup.getContext())));
        } else {
            return new OtherViewHolder(ItemRegimenRecordOtherBinding.inflate(LayoutInflater.from(viewGroup.getContext())));
        }
    }

    @Override
    public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder viewHolder, final int position) {
        if (viewHolder instanceof ChengyaoViewHolder holder) {
            holder.bindData(mDatas.get(position));
        } else if (viewHolder instanceof ShuyeViewHolder shuyeViewHolder) {
            shuyeViewHolder.bindData(mDatas.get(position));
        } else {
            OtherViewHolder otherViewHolder = (OtherViewHolder) viewHolder;
            otherViewHolder.bindData(mDatas.get(position));
        }
    }

}
